package com.ipt.frm.pw;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class PrimerServlet
 * 
 * @param <E>
 */
public class RequestInfo<E> extends MasterServlet {

	private static final long serialVersionUID = -7222970621860173660L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RequestInfo() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<body>");
		out.println("<head>");
		out.println("<title>Request Information Example por GET</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h3>Request Information Example</h3>");
		out.println("<b>Method:</b> " + request.getMethod() + "<br />");
		out.println("<b>Request URI:</b> " + request.getRequestURI() + "<br />");
		out.println("<b>Protocol:</b> " + request.getProtocol() + "<br />");
		out.println("<b>PathInfo:</b> " + request.getPathInfo() + "<br />");
		out.println("<b>Remote Address:</b> " + request.getRemoteAddr()
				+ "<br />");
		out.println("<b>Attribute Names:</b> <br />");
		out.println("	<b> Attributte 1: </b>"
				+ request.getParameter("param1").toString() + "<br />");
		out.println("	<b> Attributte 2: </b>"
				+ request.getParameter("param2").toString() + "<br />");
		out.println("<b>Auth Type: </b>" + request.getAuthType() + "<br />");
		out.println("<b>Character Encoding: </b>"
				+ request.getCharacterEncoding() + "<br />");
		out.println("</body>");
		out.println("</html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Puenteado a las peticiones por doGet
		// doGet(request, response);
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("	<head>");
		out.println("		<title>Request Information Example</title>");
		out.println("	</head>");
		out.println("	<body>");
		out.println("		<h3>Request Information Example por POST</h3>");
		out.println("		<b>Method:</b> " + request.getMethod() + "<br />");
		out.println("		<b>Request URI:</b> " + request.getRequestURI()
				+ "<br />");
		out.println("		<b>Protocol:</b> " + request.getProtocol() + "<br />");
		out.println("		<b>PathInfo:</b> " + request.getPathInfo() + "<br />");
		out.println("		<b>Remote Address:</b> " + request.getRemoteAddr()
				+ "<br />");
		out.println("		<b>Attribute Names:</b>");
		out.println("		<ol>");

		Map params = request.getParameterMap();
		Iterator it = params.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry e = (Map.Entry) it.next();
			out.println("<li><b> Parametro</b> [" + e.getKey() + "]: "
					+ e.getValue() + "</li>");
		}
		/*
		 * Enumeration<E> enume = request.getHeaderNames(); while
		 * (enume.hasMoreElements()) { String name = (String)
		 * enume.nextElement(); String value = request.getHeader(name);
		 * out.println("			<li><b>" + name + ":</b> " + value + "</li>"); }
		 */
		/*
		 * out.println("			<li><b> Attributte Name: </b>" +
		 * request.getParameter("nombre").toString() + "</li>");
		 * out.println("		<li><b> Attributte Pass: </b>" +
		 * request.getParameter("pass").toString() + "</li>");
		 * 
		 * String opciones = request.getParameter("opciones");
		 * out.println("		<li><b> Attributte opciones: </b>" + opciones +
		 * "</li>");
		 */
		out.println("		</ol>");
		out.println("<b>Auth Type: </b>" + request.getAuthType() + "<br />");
		out.println("<b>Character Encoding: </b>"
				+ request.getCharacterEncoding() + "<br />");

		HttpSession session = request.getSession(true);
		// Guardar en sesion el atributo recogido por formulario
		if (session.getAttribute("username") != null) {
			session.setAttribute("user", request.getParameter("nombre"));
		}

		// print session info

		Date created = new Date(session.getCreationTime());
		Date accessed = new Date(session.getLastAccessedTime());
		out.println("ID " + session.getId() + "<br />");
		out.println("Created: " + created + "<br />");
		out.println("Last Accessed: " + accessed + "<br />");

		out.println("</body>");
		out.println("</html>");

	}
}
